package com.mgface.framework.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.mgface.framework.utility.ProjectContext;

/**
 * 做一些公共操作的类
 * 
 * @author yuxiang
 *
 */
public class AssignCommonInterceptor extends HandlerInterceptorAdapter {
	private String projectName;

	public AssignCommonInterceptor(String projectName) {
		this.projectName = projectName;
	}

	private static final Logger log = LogManager.getLogger(AssignCommonInterceptor.class);

	/**
	 * 在业务处理器处理请求、执行完成后,生成视图之前执行的动作<br/>
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {
		log.info("后置处理方法为thymeleaf添加context,是否添加[{}]", modelAndView != null);
		if (modelAndView != null) {
			String basePath = ProjectContext.getBasePath(request, response);
			basePath = basePath + "/" + this.projectName;
			log.info("Context值为[{}]", basePath);
			modelAndView.addObject("context", basePath);
		}
	}
}
